代數的 data 型 in Go
代數的 data 型 (ADT)をGoGo.iconで表現するには?
直積型 (product type) は type embed
The Go Programming Language Specification - The Go Programming Language#Embedded interfaces¶
埋め込む
code:go
type A struct{}
type B struct{}
type AAndB struct {
A
B
}
明示
code:go
type A struct {
Aa int64
}
type B struct {
Bb string
}
type AAndB struct {
Aa int64
Bb string
}
直和型 (sum type) は type set of a union
An Introduction To Generics - The Go Programming Language#:~:text=interface{ int|string|bool }
The Go Programming Language Specification - The Go Programming Language#:~:The type set of a union
code:go
type A struct{}
type B struct{}
type AOrB interface {
A | B
}
func fT AOrB(x T) {
switch v := any(x).(type) {
case A:
// treat v as a A
case B:
// treat v as a B
}
}
兩方の型に共通するものを型 cast せずに使へんかね?
runtime なら
code:go
switch v := x.(type) {
case A:
// treat v as a A
case B:
// treat v as a B
default:
panic(fmt.Errorf("%v is a %T", x, v))
}
Coproduct (餘積) を作るか?
函數型ドメインモデリング 第12章 永續化 Go